package com.frxuexi.park2302.my

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.MyApplyEntity
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.PatrolBean
import com.frxuexi.lib_base.entity.RepairBean
import com.frxuexi.lib_base.entity.VisitBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.databinding.ActivityMyApplyBinding
import com.frxuexi.park2302.my.adapter.MyApplyAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding, MyIntent, MyViewModel>() {
    override val binding: ActivityMyApplyBinding by lazy {
        ActivityMyApplyBinding.inflate(
            layoutInflater
        )
    }
    override val vm: MyViewModel by viewModels<MyViewModel>()
    val myApplyAdapter by lazy { MyApplyAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
                                val parkBean = it.data as ParkBeanItem
                                myApplyAdapter.data.add(
                                    MyApplyEntity(
                                        "车位申请",
                                        parkBean.spaceStatus,
                                        "申请${parkBean.areaName} ${parkBean.spaceNumber}车位",
                                        "2025-07-05 13:50:00"
                                    )
                                )
                                myApplyAdapter.notifyDataSetChanged()
                            }

                            RequestType.MYVISIT -> {
                                val visitBean = it.data as VisitBean
                                visitBean.forEach {
                                    myApplyAdapter.data.add(
                                        MyApplyEntity(
                                            "来访申请",
                                            it.visitStatus,
                                            "访客：${it.visitorName}(1人)",
                                            it.appointmentTime
                                        )
                                    )
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }

                            RequestType.MYREPAIR -> {
                                val repairBean = it.data as RepairBean
                                repairBean.forEach {
                                    myApplyAdapter.data.add(
                                        MyApplyEntity(
                                            "报修申请",
                                            if (it.processingStatus == 0) "待处理" else if (it.processingStatus == 1) "处理中" else "已完成",
                                            it.faultDescription,
                                            it.repairTime
                                        )
                                    )
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYPATROL->{
                                val patrolBean = it.data as PatrolBean
                                patrolBean.forEach {
                                    myApplyAdapter.data.add(
                                        MyApplyEntity(
                                            "巡更申请",
                                            if(it.patrolStatus=="0") "未巡检" else if(it.patrolStatus=="1")"巡检中" else  "已完成",
                                            "${it.taskName}巡更申请",
                                            it.patrolTime
                                        )
                                    )
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }

                            else -> {}

                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvMy.apply {
            adapter = myApplyAdapter
            layoutManager = LinearLayoutManager(this@MyApplyActivity)
        }
        val uname = SPUtils.getInstance().getString(Const.UNAME)
        val uid = SPUtils.getInstance().getInt(Const.UID)
        vm.sendIntent(MyIntent.getMyPark(uid))
        vm.sendIntent(MyIntent.getMyVisit(uname))
        vm.sendIntent(MyIntent.getMyRepair(uname))
        vm.sendIntent(MyIntent.getMyPatrol(uid))

        binding.myParkCon.setOnClickListener {
            myApplyAdapter.data.clear()
            vm.sendIntent(MyIntent.getMyPark(uid))
        }

        binding.myVisitCon.setOnClickListener {
            myApplyAdapter.data.clear()
            vm.sendIntent(MyIntent.getMyVisit(uname))
        }

        binding.myPatrolCon.setOnClickListener {
            myApplyAdapter.data.clear()
            vm.sendIntent(MyIntent.getMyPatrol(uid))
        }

        binding.myRepairCon.setOnClickListener {
            myApplyAdapter.data.clear()
            vm.sendIntent(MyIntent.getMyRepair(uname))
        }
    }

}